/*
 * BuildingManager.hpp
 *
 *  Created on: 2013-1-20
 *      Author: heqiu
 */

#ifndef BUILDINGMANAGER_H_
#define BUILDINGMANAGER_H_

#include <string>
#include <map>
#include "Guud.h"
using namespace std;
using namespace guud;

class BuildingType {
public:
	string m_sName;
	uint32_t m_iType;
	bool m_bLoaded;
	GuudSurface * m_pGuudSurface;
	BuildingType() {
		m_iType = 0;
		m_bLoaded = false;
		m_pGuudSurface = NULL;
	}
	~BuildingType() {
		if (m_pGuudSurface != NULL) {
			delete m_pGuudSurface;
			m_pGuudSurface = NULL;
		}
	}
	void loadImage() {
		if (m_bLoaded) {
			return;
		}
		m_pGuudSurface = new GuudSurface();
		bool bFlag = m_pGuudSurface->loadImage(m_sName.c_str());
		if (!bFlag) {
			string sErrInfo = "m_pGuudSurface->loadImage:" + m_sName;
			guud_exit(__FILE__, __LINE__, bFlag, sErrInfo.c_str());
		}
		m_bLoaded = true;
	}
};
class BuildingTypeManager {
public:
	map<uint32_t, BuildingType *> m_mapPtr;

	BuildingTypeManager() {

	}
	~BuildingTypeManager() {
		map<uint32_t, BuildingType *>::iterator it;
		for (it = m_mapPtr.begin(); it != m_mapPtr.end(); it++) {
			delete it->second;
		}
		m_mapPtr.clear();
	}
	bool addBuildingTypePtr(BuildingType * ptr) {
		if (m_mapPtr.find(ptr->m_iType) != m_mapPtr.end()) {
			return false;
		}
		m_mapPtr[ptr->m_iType] = ptr;
		return true;
	}
};

class BuildingManager {
public:
	map<uint32_t, UnitBuilding *> m_mapPtr;

	BuildingManager() {

	}
	~BuildingManager() {
		map<uint32_t, UnitBuilding *>::iterator it;
		for (it = m_mapPtr.begin(); it != m_mapPtr.end(); it++) {
			delete it->second;
		}
		m_mapPtr.clear();
	}
	bool addBuildingPtr(UnitBuilding * ptr) {
		if (m_mapPtr.find(ptr->m_iId) != m_mapPtr.end()) {
			return false;
		}
		m_mapPtr[ptr->m_iId] = ptr;
		return true;
	}
	bool delBuilding(uint32_t uiId) {
		map<uint32_t, UnitBuilding *>::iterator it;
		it = m_mapPtr.find(uiId);
		if (it == m_mapPtr.end()) {
			return false;
		} else {
			delete it->second;
			return true;
		}
	}
	void loadImage();
	void addToMap(UnitMap & stMap) {
		map<uint32_t, UnitBuilding *>::iterator it;
		for (it = m_mapPtr.begin(); it != m_mapPtr.end(); it++) {
			stMap.setBuilding(*it->second);
		}
	}

	UnitBuilding * getBuilding(uint32_t uiId) {
		map<uint32_t, UnitBuilding *>::iterator it;
		it = m_mapPtr.find(uiId);
		if (it == m_mapPtr.end()) {
			return NULL;
		}
		return it->second;
	}

};

#endif /* BUILDINGMANAGER_H_ */
